package com.younion.action.consumer.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IMobileMessageService;
import com.younion.model.MobileMessageOperatorResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.vo.MobileMessageVO;

public class MobileMessageConsumer extends AbstractRequestConsumer {
	private IMobileMessageService mobileMessageService = null;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return addMessage(request, response);
	}

	public String addMessage(HttpServletRequest request,
			HttpServletResponse response) {
		MobileMessageOperatorResult mobileMessageOperatorResult = new MobileMessageOperatorResult();
		String userToken = request.getParameter("user_token");
		String content = request.getParameter("content");
		Map paramMap = new HashMap();
		paramMap.put("userToken", userToken);
		ResultObject resultObject = new ResultObject();
		try {
			MobileMessageVO lastMobileMessageVO = mobileMessageService
					.getLastByMap(paramMap);
			if (lastMobileMessageVO != null) {
				if (new Date().getTime()
						- lastMobileMessageVO.getCreateTime().getTime() <= 0.5 * 60 * 1000) {// 控制留言频率为半分钟
					resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
					mobileMessageOperatorResult.setSuccessful("0");
					resultObject.setMsg(ApiConstant.MESSAGE_ADD_TOOFAST);
					resultObject.setResult(mobileMessageOperatorResult);
				} else {
					save(userToken, content);
					resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
					mobileMessageOperatorResult.setSuccessful("1");
					resultObject.setMsg(ApiConstant.MESSAGE_ADD_SUCCESS);
					resultObject.setResult(mobileMessageOperatorResult);
				}
			} else {
				save(userToken, content);
				resultObject.setStatus(ApiConstant.SUCCESS_STATUS);
				mobileMessageOperatorResult.setSuccessful("1");
				resultObject.setMsg(ApiConstant.MESSAGE_ADD_SUCCESS);
				resultObject.setResult(mobileMessageOperatorResult);
			}
		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			mobileMessageOperatorResult.setSuccessful("0");
			resultObject.setResult(mobileMessageOperatorResult);
			e.printStackTrace();
		}
		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	private void save(String userToken, String content) throws Exception {
		MobileMessageVO mobileMessageVO = new MobileMessageVO();
		mobileMessageVO.setContent(content);
		mobileMessageVO.setUserToken(userToken);
		mobileMessageVO.setCreateTime(new Date());
		mobileMessageService.save(mobileMessageVO);
	}

	public IMobileMessageService getMobileMessageService() {
		return mobileMessageService;
	}

	public void setMobileMessageService(
			IMobileMessageService mobileMessageService) {
		this.mobileMessageService = mobileMessageService;
	}

}
